﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models;
using DataManager;
using HRMS_Model;
using Lib.Web.Mvc.JQuery.JqGrid;
using System.Globalization;
using HRMS.Models.ModelBinders;

namespace HRMS.Controllers
{
    public class DesignationController : Controller
    {
        //
        // GET: /Designation/
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Designations(JqGridRequest request)
        {
            //get the count of roles
            var totalRecords = DesignationsModels.GetDesignationsCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get Designations
            var designations = DesignationsModels.GetDesignations(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var designation in designations)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(designation.DesignationId), new List<object>() { 
                                    designation.DesignationId,
                designation.DesignationName
                }));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertDesignation(Designation designationModel)
        {
            return Json(DesignationsModels.AddDesignation(designationModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateDesignation([ModelBinder(typeof(DesignationModelBinder))]Designation designationModel)
        {
            return Json(DesignationsModels.UpdateDesignation(designationModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteDesignation(int id)
        {
            return Json(DesignationsModels.DeleteDesignation(id));
        }

        //[HttpPost]
        //public ActionResult Index(Designation designation)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var desig = new Designation
        //        {
        //            DesignationName = designation.DesignationName

        //        };

        //        Designations.AddDesignation(desig);
        //        return RedirectToAction("Index", "Designation");
        //    }
        //    return View();
        //}
        //public ActionResult DesignationData(string rows, string page, string sidx, string sord)
        //{
        //    var designations = HRMS.Models.DesignationsModels.GetDesignations().ToList();
        //    var totalRecords = Convert.ToInt32(designations.Count());
        //    var jsonData = new
        //    {
        //        total = 1,
        //        page = Convert.ToInt32(page),
        //        records = totalRecords,
        //        rows = (
        //          from designation in designations
        //          select new
        //          {
        //              id = designation.DesignationId,
        //              cell = new string[] {                         
        //                  designation.DesignationName
        //                    }
        //          })
        //    };
        //    return Json(jsonData, JsonRequestBehavior.AllowGet);
        //}

    }
}
